﻿@page "/projectlist/{EnvironmentClusterId:int}"
@inherits MasaComponentBase


<div class="full-height rounded-4" style="overflow: auto;">
    @if (_projects.Any())
    {
        <MExpansionPanels Class="project-list" Flat Accordion @key="@($"{EnvironmentClusterId}{TeamId}")">
            @foreach (var project in _projects)
            {
                var apps = _apps.Where(app => app.ProjectId == project.Id ).ToList();
                LatestReleaseConfigModel? latest = null;
                var bizLatestRelease = _bizLatestReleaseConfig.FirstOrDefault(x => x.ProjectId == project.Id);
                var appLatestRelease = _appLatestReleaseConfig.Where(l => apps.Any(a => a.Id == l.AppId)).MaxBy(x => x.LastPublishTime);
                if (bizLatestRelease != null && appLatestRelease != null)
                {
                    latest = bizLatestRelease;
                    if (bizLatestRelease.LastPublishTime <= appLatestRelease.LastPublishTime)
                    {
                        latest = appLatestRelease;
                    }
                }
                else
                {
                    latest = bizLatestRelease ?? appLatestRelease;
                }
                <MExpansionPanel Class="rounded-4 mb-4">
                    <MExpansionPanelHeader Style="height: 56px;" Class="my-6">
                        <ChildContent>
                            <span style="display:flex;align-items:center; margin-right:10px;">
                                <div class="emphasis--text h6">
                                    <MHover Context="hoverContext">
                                        <span @onclick:stopPropagation @onclick="()=>HandleProjectNameClick(project.Id)" @attributes="hoverContext.Attrs" class="@(hoverContext.Hover ? "primary--text":"")">
                                            @project.Name
                                        </span>
                                    </MHover>
                                    <div class="mt-1 regul ar3--text body2 d-flex" style="padding:0">
                                        @if (!string.IsNullOrEmpty(project.LabelName))
                                        {
                                            string backgroundColor;
                                            switch (project.LabelCode.ToLower())
                                            {
                                                case "basicability":
                                                    backgroundColor = "#37D7AD";
                                                    break;
                                                case "operator":
                                                    backgroundColor = "#FFB547";
                                                    break;
                                                case "datafactory":
                                                    backgroundColor = "#5FB9FF";
                                                    break;
                                                default:
                                                    backgroundColor = "#BDBDBD";
                                                    break;
                                            }
                                            <div class="d-flex justify-center mr-2" style="border-radius:6px; color:#fff; background-color:@backgroundColor; padding:0 4px; height: fit-content;">
                                                @project.LabelName
                                            </div>
                                        }
                                        <span class="regular3--text body2">@project.Identity</span>
                                        <span class="ml-2 regular3--text body2">|</span>
                                        <sapn class="ml-2 regular3--text body2">@T("AppCount"): @apps.Count</sapn>
                                        <LatestConfigPublish LatestReleaseConfigModel="latest"></LatestConfigPublish>
                                    </div>
                                </div>
                            </span>
                        </ChildContent>
                        <ActionsContent>
                            <SIcon Color="#485585" IsDefaultToolTip="false">
                                mdi-chevron-down
                            </SIcon>
                        </ActionsContent>
                    </MExpansionPanelHeader>
                    <MExpansionPanelContent>
                        <MRow>
                            @if (apps.Any())
                            {
                                foreach (var app in apps.Take(3))
                                {
                                    var appLatest = _appLatestReleaseConfig.FirstOrDefault(x => x.AppId == app.Id);
                                    <MCol Lg="4" Md="6" Sm="12">
                                        <AppCard 
                                            OnClick="() => HandleAppCardClick(new ConfigComponentModel(app.Id, app.EnvironmentClusters[0].Id, ConfigObjectType.App, app.ProjectId))"
                                            OnPinClick="() => HandleAppPinClick(app)" IsPin="app.IsPinned" App="app" EnvironmentClusters="app.EnvironmentClusters" LatestReleaseConfig="appLatest">
                                            <MChipGroup @key="app.EnvironmentClusters" Class="env-cluster-chip-group" ActiveClass="primary--text">
                                                @foreach (var item in app.EnvironmentClusters)
                                                {
                                                    <EnvClusterChip OnClick="() => HandleAppCardClick(new ConfigComponentModel(app.Id, item.Id, ConfigObjectType.App, app.ProjectId))"
                                                                    Style="margin:0 12px 1px 0;"
                                                                    EnvironmentName="@item.EnvironmentName"
                                                                    EnvironmentColor="@item.EnvironmentColor"
                                                                    ClusterName="@item.ClusterName"/>
                                                }
                                            </MChipGroup>
                                        </AppCard>
                                    </MCol>
                                }
                            }
                            else
                            {
                                <SEmptyPlaceholder />
                            }
                        </MRow>
                        <div class="d-flex justify-space-between align-flex-end mt-4 mb-2">
                            @{
                                var team = _allTeams.FirstOrDefault(team => team.Id == project.TeamId) ?? new();
                                <MTooltip Right>
                                    <ActivatorContent>
                                        <MAvatar Size="24" @attributes="@context.Attrs">
                                            <MImage Src="@team.Avatar"></MImage>
                                        </MAvatar>
                                    </ActivatorContent>
                                    <ChildContent>
                                        <span>@team.Name</span>
                                    </ChildContent>
                                </MTooltip>
                            }
                            <MHover>
                                @{
                                    var mClass = $"{(context.Hover ? "primary--text" : "regular2--text")}";
                                    <div @onclick="()=>HandleProjectNameClick(project.Id)" @attributes="context.Attrs" class="@($"hover-pointer btn {mClass}")">
                                        @T("More")
                                        <SIcon Class="@($"ml-1 {mClass}")" Size="18">mdi-chevron-right</SIcon>
                                    </div>
                                }
                            </MHover>
                        </div>
                    </MExpansionPanelContent>
                </MExpansionPanel>
            }
        </MExpansionPanels>
    }
    else
    {
        <SEmptyPlaceholder />
    }
</div>

<MOverlay Class="rounded-4" Value="_showProcess" Absolute Color="#fff" Opacity="1.0">
    <MProgressCircular Width="4" Indeterminate Size="48" Color="primary"></MProgressCircular>
</MOverlay>